package com.mjfeed.controller.admin;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mjfeed.controller.BaseController;
import com.mjfeed.entity.Member;
import com.mjfeed.service.MemberService;
import com.mjfeed.util.Format;
import com.mjfeed.util.Md5Util;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;

/**
 * @description 会员
 * @author zhaominglei
 * @date 2014年10月7日
 */
@Controller("adminMemberController")
@RequestMapping("/admin/member")
public class MemberController extends BaseController {
	@Resource(name="memberService")
	private MemberService memberService;
	
	@RequestMapping(value = "/list", method=RequestMethod.GET)
	public String list(ParamBean paramBean, HttpServletRequest request, HttpServletResponse response, Model model){
		paramBean.setUrl(getBasePath(request) + "admin/member/list.shtml");
		int p = (Format.null2Zero(request.getParameter("p")) == 0) ? 1 : Format.null2Zero(request.getParameter("p"));
		paramBean.setPage(p);
		Pagination<Member> pagination = memberService.getPagination(paramBean);
		request.setAttribute("pagination", pagination);
		return "admin/member/list";
	}
	
	@RequestMapping(value = "/add",method=RequestMethod.GET)
	public String add(HttpServletRequest request, HttpServletResponse response, Model model){
		return "admin/member/add";
	}
	
	@RequestMapping(value = "/save", method=RequestMethod.POST)
	public String save(Member member, HttpServletRequest request, HttpServletResponse response, Model model){
		member.setPassword(Md5Util.encrypt(member.getPassword()));
		member.setIsEnabled((member.getIsEnabled() != null && member.getIsEnabled()) ? true : false);
		member.setIsLocked(false);
		member.setLoginFailureCount(0);
		member.setLockedDate(null);
		member.setRegisterIp(super.getRemoteIpAddr(request));
		member.setLoginIp(super.getRemoteIpAddr(request));
		member.setLoginDate(new Date());
		memberService.save(member);
		return "redirect:/admin/member/list.shtml";
	}
	
	@RequestMapping(value = "/edit",method=RequestMethod.GET)
	public String edit(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("member", memberService.getById(Member.class, id));
		return "admin/member/edit";
	}
	
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public String update(Member member, HttpServletRequest request, HttpServletResponse response, Model model){
		Member pMember = memberService.getById(Member.class, member.getId());
		pMember.setEmail(member.getEmail());
		pMember.setName(member.getName());
		pMember.setMobile(member.getMobile());
		pMember.setIsEnabled((member.getIsEnabled() != null && member.getIsEnabled()) ? true : false);
		memberService.update(pMember);
		return "redirect:/admin/member/list.shtml";
	}
	
	@RequestMapping(value = "/view",method=RequestMethod.GET)
	public String view(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("member", memberService.getById(Member.class, id));
		return "admin/member/view";
	}
	
	@RequestMapping(value = "/editpassword", method=RequestMethod.GET)
	public String editpassword(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		request.setAttribute("member", memberService.getById(Member.class, id));
		return "admin/member/editpassword";
	}
	
	@RequestMapping(value = "/updatepassword", method=RequestMethod.POST)
	public String updatepassword(Long id, HttpServletRequest request, HttpServletResponse response, Model model){
		String pPassword = request.getParameter("pPassword");
		String nPassword = request.getParameter("nPassword");
		String cfnPassword = request.getParameter("cfnPassword");
		if (StringUtils.isEmpty(pPassword) || StringUtils.isEmpty(nPassword) || StringUtils.isEmpty(cfnPassword)) {
			return ERROR_VIEW;
		}
		if (!cfnPassword.equals(nPassword)) {
			return ERROR_VIEW;
		}
		Member pMember = memberService.getById(Member.class, id);
		if (!Md5Util.encrypt(pPassword).equals(pMember.getPassword())) {
			return ERROR_VIEW;
		}
		pMember.setPassword(Md5Util.encrypt(nPassword));
		memberService.update(pMember);
		return "redirect:/admin/member/list.shtml";
	}
}
